package com.lemon.smartsafetynew.ui.fragment.record;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.ApplicationInfo;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.record.MajorHazardNewActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordAmmoniaActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordBaseInfoActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordCertificateActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordEmergencyActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordEmergencyPlanActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordMetalActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordOccupationActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordSafeTrainingActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordSpaceActivity;
import com.lemon.smartsafetynew.ui.fragment.base.BaseSearchPullToRefreshFragment;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.me.MeCompanyVo;
import com.lemon.smartsafetynew.entity.record.FunctionVo;
import com.lemon.smartsafetynew.net.services.RecordServices;
import com.lemon.smartsafetynew.ui.activity.record.AirActivity;
import com.lemon.smartsafetynew.ui.activity.record.DustActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordEquipmentActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordHighDangerActivity;
import com.lemon.smartsafetynew.ui.activity.record.MaterielActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordHiddenActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordPatrolActivity;
import com.lemon.smartsafetynew.ui.activity.record.SwitchBoxActivity;
import com.lemon.smartsafetynew.ui.activity.record.RecordTankActivity;
import com.lemon.smartsafetynew.ui.adapter.me.MeCompanyListAdapter;
import com.lemon.smartsafetynew.ui.adapter.record.RecordCompanyListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;

import java.util.ArrayList;
import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * 企业列表
 * Created by Andrew on 2017/10/21.
 */

public class CompanySearchFragment extends BaseSearchPullToRefreshFragment<MeCompanyVo, List<MeCompanyVo>>
        implements RecyclerTClickListener<MeCompanyVo>, View.OnClickListener {

    private String search_params = "";
    private static FunctionVo functionVo;
    private List<MeCompanyVo> historyList = new ArrayList<>();
    private RecordCompanyListAdapter recordCompanyListAdapter;

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_DOCS_VO)) {
                functionVo = (FunctionVo) bundle.getSerializable(KeyConstants.KEY_DOCS_VO);
            }
        }
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    public static CompanySearchFragment newInstance(FunctionVo infoDo) {

        Bundle args = new Bundle();
        args.putSerializable(KeyConstants.KEY_DOCS_VO, infoDo);
        CompanySearchFragment fragment = new CompanySearchFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onPullRefresh() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("type", functionVo.getType());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);
        params.put("company_name", search_params);
        RecordServices
                .init()
                .getCompanyListByGridType(RefreshType.REFRESH, params);
    }

    @Override
    public void onLoadMore() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("type", functionVo.getType());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);
        params.put("company_name", search_params);
        RecordServices
                .init()
                .getCompanyListByGridType(RefreshType.LOAD_MORE, params);
    }


    @Override
    public LemonRcvAdapter<MeCompanyVo> getLemonAdapter() {
        if (ApplicationInfo.RecordFunctionEnum.LOCAL_EMERGENCY_PLAN_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            return new MeCompanyListAdapter(mDataList, this, true);
        } else {
            return new MeCompanyListAdapter(mDataList, this, false);
        }
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_SEARCH_COMPANY_BY_GRID_TYPE;
    }

    @Override
    public void onItemClick(MeCompanyVo data, int position) {
        Bundle bundle = new Bundle();
        if (!UserTypeUtil.isGovType(AccountUtil.getUserType())) {
            data.setIfShowReturn(false);
        } else {
            data.setIfShowReturn(true);
        }
        if (ApplicationInfo.RecordFunctionEnum.LOACL_PATROL_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 检查点位
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordPatrolActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOACL_HIDDEN_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 隐患记录
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordHiddenActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.WEB_NORMAL_INFO_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 基本信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordBaseInfoActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.WEB_PERSON_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 企业人员
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordCompanyPeopleActivity.class);

        } else if (ApplicationInfo.RecordFunctionEnum.WEB_MAJOR_HAZARD_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 重大危险源
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, MajorHazardNewActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.WEB_SPECIAL_EQUIPMENT_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 特种设备
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordEquipmentActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.WEB_MATERIALS_INFO_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 物料信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, MaterielActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.WEB_TANKS_INFO_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 储罐信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordTankActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.WEB_DUST_INFO_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 粉尘信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, DustActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.WEB_CONFINED_SPACE_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 受限空间
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordSpaceActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_ELECTRICAL_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 配电箱信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, SwitchBoxActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_AIR_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 可燃气体
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, AirActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_HIGH_DANGER_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 高危工艺
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordHighDangerActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_METALLURGY_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 冶金信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordMetalActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_AMMONIA_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 液氨信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordAmmoniaActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_OCCUPATIONA_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 职业病危害信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordOccupationActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_EMERGENCY_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 应急物资
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordEmergencyActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_SPECISL_TASK_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 特种作业操作证人员
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordCertificateActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_COMPANY_TRAINING_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            // 企业安全培训信息
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordSafeTrainingActivity.class);
        } else if (ApplicationInfo.RecordFunctionEnum.LOCAL_EMERGENCY_PLAN_TYPE.equals(functionVo.getRecordFunctionEnum())) {
            //应急预案
            bundle.putSerializable(KeyConstants.KEY_COMPANY_VO, data);
            UIHelper.showActivity(getmContext(), bundle, RecordEmergencyPlanActivity.class);
        }

    }

    @Override
    public void dataList(List<MeCompanyVo> data) {
        if(data != null && data.size() > 0) {
            tvCompanyTotal.setText("" + data.get(0).getTotalNum());
        }else{
            tvCompanyTotal.setText("0");
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        recyclerView.setVisibility(View.GONE);
        emptyImage.setVisibility(View.GONE);
        mMultiStateView.setVisibility(View.VISIBLE);
        llInfoCount.setVisibility(View.VISIBLE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getmContext(), LinearLayoutManager.VERTICAL, false);
        recordCompanyListAdapter = new RecordCompanyListAdapter(historyList, this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(recordCompanyListAdapter);
        gridNameTv.setVisibility(View.GONE);
        txtClearn.setOnClickListener(this);
        lineRight.setOnClickListener(this);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == KeyEvent.ACTION_DOWN || actionId == EditorInfo.IME_ACTION_DONE) {
                    search_params = editText.getText().toString().trim();
                    query(false);

                }
                return true;
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                search_params = s.toString().trim();

                query(true);

                if (!"".equals(s.toString())) {
                    txtClearn.setVisibility(View.VISIBLE);
                } else {
                    txtClearn.setVisibility(View.GONE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.lineRight:
                //点击搜索按钮
                search_params = editText.getText().toString().trim();
                query(false);
                break;

            case R.id.txtClearn:
                editText.getText().clear();
                break;

            default:
                break;
        }
    }

    /**
     * 搜索
     * 网格点信息    ==Null
     * gridInfoDo---------->  在输入搜索关键字时，输入数据为空串则显示历史，不为空搜索关键词
     * |
     * | != Null
     * |
     * \|/
     * *
     * 当点击搜索按钮后搜索
     */
    private void query(boolean realTime) {
        if (!realTime) {
            // 获取刷新
            doPullRefresh();
        } else {
            if ("".equals(search_params)) {
                // 当搜索内容为空时，显示搜索一次全部列表
                doPullRefresh();
            }
        }
    }
}
